//
//  Possession.m
//  RandomPossessions
//
//  Created by Xie Yi on 12-7-22.
//  Copyright (c) 2012年 Free. All rights reserved.
//

#import "Possession.h"

@implementation Possession


/*
-(void)setPossessionName:(NSString *)str
{
    [str retain];
    [possessinName release];
    possessinName = str;
}
-(NSString *)possessionName
{
    
    return possessinName;
}

-(void)setSerialNumber:(NSString *)str
{
    [str retain];
    [serialNumber release];
    serialNumber = str;
}
-(NSString *)serialNumber
{
    return serialNumber;
}

-(void)setValueInDollars:(int)i
{
    valueInDollars = i;
}
-(int)valueInDollars
{
    return valueInDollars;
}

-(NSDate *)dateCreated
{
    return dateCreated;
}
 */

@synthesize possessionName,serialNumber,valueInDollars,dateCreated;

+(id)randomPossession
{
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy",@"Rusty",@"Shiny",nil];
    NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear",@"Spork",@"Mac",nil];
    
    int adjectiveIndex = rand() % [randomAdjectiveList count];
    int nounIndex = rand() % [randomNounList count];

    
    NSString *randomName = [NSString stringWithFormat:@"%@ %@", 
                            [randomAdjectiveList objectAtIndex:adjectiveIndex],
                            [randomNounList objectAtIndex:nounIndex]];
    
    int randomValue = rand() % 100;
    
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0' + rand() % 10,
                                    'A' + rand() % 26,
                                    '0' + rand() % 10,
                                    'A' + rand() % 26,
                                    '0' + rand() % 10];
    
    Possession *newPossession = [[self alloc] initWithPossession:randomName 
                                              valueInDollars:randomValue 
                                              serialNmber:randomSerialNumber];
    return [newPossession autorelease];
    
}

-(id)initWithPossession:(NSString *)name valueInDollars:(int)value serialNmber:(NSString *)sNumber
{
    self = [super init];
    if(self){
        [self setPossessionName:name];
        [self setValueInDollars:value];
        [self setSerialNumber:sNumber];
        dateCreated = [[NSDate alloc] init];
    }
    return self;
}

-(id)init
{
    return [self initWithPossession:@"Possession" valueInDollars:0 serialNmber:@""];
}

-(NSString *)description
{
    /*
     NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@", possessinName,serialNumber,valueInDollars,dateCreated];
    return [descriptionString autorelease];
     */
    return [NSString stringWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
            possessionName,serialNumber,valueInDollars,dateCreated];        
}

-(void)dealloc
{
    [possessionName release];
    [serialNumber release];
    [dateCreated release];
    [super dealloc];
}

@end
